class AudioPlayer {
    constructor() {
        this.audioList = [];
        this.currentIndex = 0;
        this.audio = uni.createInnerAudioContext();
        this.isPlaying = false;

        // 监听音频播放结束事件
        this.audio.onEnded(() => this.playNext());
    }

    // 添加音频文件到播放列表
    addAudio(audioPath) {
        this.audioList.push(audioPath);
        // 如果没有正在播放的音频，立即开始播放
        if (!this.isPlaying && this.currentIndex >= this.audioList.length - 1) {
            this.playNext();
        }
    }

    // 播放下一个音频文件
    playNext() {
        if (this.currentIndex < this.audioList.length) {
            this.audio.src = this.audioList[this.currentIndex];
            this.audio.play();
            this.isPlaying = true;
            this.currentIndex++;
        } else {
            this.isPlaying = false;
        }
    }
}

// 导出类
export const audioPlayer = new AudioPlayer();